{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "共48种着色方案\n",
      "[[1. 2. 3. 4. 1.]\n",
      " [1. 2. 3. 4. 3.]\n",
      " [1. 2. 4. 3. 1.]\n",
      " [1. 2. 4. 3. 4.]\n",
      " [1. 3. 2. 4. 1.]\n",
      " [1. 3. 2. 4. 2.]\n",
      " [1. 3. 4. 2. 1.]\n",
      " [1. 3. 4. 2. 4.]\n",
      " [1. 4. 2. 3. 1.]\n",
      " [1. 4. 2. 3. 2.]\n",
      " [1. 4. 3. 2. 1.]\n",
      " [1. 4. 3. 2. 3.]\n",
      " [2. 1. 3. 4. 2.]\n",
      " [2. 1. 3. 4. 3.]\n",
      " [2. 1. 4. 3. 2.]\n",
      " [2. 1. 4. 3. 4.]\n",
      " [2. 3. 1. 4. 1.]\n",
      " [2. 3. 1. 4. 2.]\n",
      " [2. 3. 4. 1. 2.]\n",
      " [2. 3. 4. 1. 4.]\n",
      " [2. 4. 1. 3. 1.]\n",
      " [2. 4. 1. 3. 2.]\n",
      " [2. 4. 3. 1. 2.]\n",
      " [2. 4. 3. 1. 3.]\n",
      " [3. 1. 2. 4. 2.]\n",
      " [3. 1. 2. 4. 3.]\n",
      " [3. 1. 4. 2. 3.]\n",
      " [3. 1. 4. 2. 4.]\n",
      " [3. 2. 1. 4. 1.]\n",
      " [3. 2. 1. 4. 3.]\n",
      " [3. 2. 4. 1. 3.]\n",
      " [3. 2. 4. 1. 4.]\n",
      " [3. 4. 1. 2. 1.]\n",
      " [3. 4. 1. 2. 3.]\n",
      " [3. 4. 2. 1. 2.]\n",
      " [3. 4. 2. 1. 3.]\n",
      " [4. 1. 2. 3. 2.]\n",
      " [4. 1. 2. 3. 4.]\n",
      " [4. 1. 3. 2. 3.]\n",
      " [4. 1. 3. 2. 4.]\n",
      " [4. 2. 1. 3. 1.]\n",
      " [4. 2. 1. 3. 4.]\n",
      " [4. 2. 3. 1. 3.]\n",
      " [4. 2. 3. 1. 4.]\n",
      " [4. 3. 1. 2. 1.]\n",
      " [4. 3. 1. 2. 4.]\n",
      " [4. 3. 2. 1. 2.]\n",
      " [4. 3. 2. 1. 4.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def Fill(color, adj, m, i, result):\n",
    "    if i < len(color):\n",
    "        for j in range(1, m+1):\n",
    "            if j not in adj[i, :]*color:\n",
    "                color[i] = j\n",
    "                Fill(color, adj, m, i+1,result)\n",
    "                color[i] = 0\n",
    "    else:\n",
    "        result.append(color.copy())\n",
    "\n",
    "# 输入\n",
    "n, k, m = [5, 8, 4]\n",
    "u, v = [[1, 1, 1, 2, 2, 2, 3, 4],\n",
    "        [2, 3, 4, 3, 4, 5, 4, 5]]\n",
    "\n",
    "# 邻接矩阵\n",
    "adj = np.zeros((n, n))\n",
    "for i, j in zip(u, v):\n",
    "    adj[i-1, j-1] += 1\n",
    "    adj[j-1, i-1] += 1\n",
    "\n",
    "color = np.zeros(n)\n",
    "result = []\n",
    "Fill(color,adj,m,0,result)\n",
    "print(\"共{}种着色方案\".format(len(result)))\n",
    "print(np.array(result))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "bfb6b6056bbcee18ee270338479bcc9a7798e633b623cdbb3c7849c1f8787a17"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
